from flask import Blueprint, render_template, flash, redirect, url_for, request, jsonify, session, g
from flask_login import current_user, login_required

from apps.forms.album_info_form import QueryAlbumInfoForm, CreateAlbumInfoForm, GetAlbumInfoForm, UpdateAlbumInfoForm
from rules.album_info_rule import search_album_info_with_page, create_album_info, search_album_info_all, \
    describe_album_info
from rules.dy_tags_rule import query_dy_tags_with_list

album_info = Blueprint('album_info', __name__, url_prefix='/album')
ACTIVE = 'album'
ACTIVE_ITEM = 'album'
SEARCH_PAGE = 'album_info_page'


@album_info.route('/page', methods=['POST', 'GET'])
@login_required
def page():
    form = QueryAlbumInfoForm(csrf_enabled=False)
    last_search = session.get('last_search')
    page_index = 1
    title = ''
    release_year = ''
    album_status = ''

    if request.method == 'GET':
        if last_search and last_search.get('search_page', '') == SEARCH_PAGE:
            page_index = last_search.get('page_index', 1)
            if last_search.get('search_data'):
                title = last_search.get('search_data').get('title', '')
                release_year = last_search.get('search_data').get('release_year', '')
                album_status = last_search.get('search_data').get('album_status', '')

        page_index = request.args.get('page', page_index, type=int)

        title = request.args.get('title', title)
        release_year = request.args.get('release_year', release_year)
        album_status = request.args.get('album_status', album_status)

        form.title.data = title
        form.release_year.data = release_year
        form.album_status.data = album_status

    else:
        page_index = 1
        if form.title.data:
            title = form.title.data

        if form.release_year.data:
            release_year = form.release_year.data

        if form.album_status.data:
            album_status = form.album_status.data

    search_data = dict(title=title, release_year=release_year, album_status=album_status)
    last_search = dict(search_page=SEARCH_PAGE, page_index=page_index, search_data=search_data)
    session['last_search'] = last_search

    pagination = search_album_info_with_page(page_index, search_data)

    return render_template('album_info/page.html', pagination=pagination, form=form, active=ACTIVE,
                           active_item=ACTIVE_ITEM)


@album_info.route('/create', methods=['POST', 'GET'])
@login_required
def create():
    form = CreateAlbumInfoForm()
    dy_tags = query_dy_tags_with_list(dict())
    if form.validate_on_submit():
        try:
            create_album_info(form, current_user)
            flash('新增成功', 'page_success')
            return redirect(url_for('album_info.page'))
        except Exception as e:
            g.exception_occurred = e
            flash(str(e), 'page_error')

    return render_template('album_info/create.html', form=form, active=ACTIVE,
                           active_item=ACTIVE_ITEM, dy_tags=dy_tags)


@album_info.route('/index/<int:id>', methods=['GET'])
@login_required
def index(id):
    album_info_dict = search_album_info_all(id)
    if not album_info_dict:
        return redirect(url_for('album_info.page'))

    form = GetAlbumInfoForm(album_info_dict)
    return render_template('album/index.html', album_info=album_info_dict, form=form, active_item=ACTIVE_ITEM)


@album_info.route('/edit/<int:id>', methods=['POST', 'GET'])
@login_required
def edit(id):
    dy_tags = query_dy_tags_with_list(dict())
    album_info_dict = search_album_info_all(id)
    if not album_info_dict:
        return redirect(url_for('album_info.page'))

    if request.method == 'GET':
        form = UpdateAlbumInfoForm(album_info_dict)
    else:
        form = UpdateAlbumInfoForm()

    if form.validate_on_submit():
        try:
            describe_album_info(id, form, current_user)
            flash('修改成功', 'page_success')
            return redirect(url_for('album_info.index', id=id))
        except Exception as e:
            g.exception_occurred = e
            flash(str(e), 'page_error')

    return render_template('album/edit.html', album_info=album_info_dict, form=form, active_item=ACTIVE_ITEM,
                           dy_tags=dy_tags)
